package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.article.service.ArticleHomeService;
import com.heima.common.constants.ArticleConstants;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.search.vos.SearchArticleVo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;

/**
 * @author lzq
 * @data 2023/5/3 20:40
 */
@Service
public class ArticleFreemarkerServiceImpl implements ArticleFreemarkerService {
    /**
     * 生成静态文件上传到minIO中
     * @param apArticle
     * @param content
     */
    @Autowired
    private ApArticleContentMapper acMapper;
    @Autowired
    private Configuration configuration;
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private ArticleHomeService articleHomeService;
    //文章详情上传
    @Override
    @Async
    public void buildArticleToMinIO(ApArticle apArticle, String content) {
        try {
            if (content!=null){
                Template template = configuration.getTemplate("article.ftl");
                //获取输出流
                StringWriter out = new StringWriter();
                HashMap<String, Object> map = new HashMap<>();
                map.put("content", JSONArray.parseArray(content));//JSONArray.parseArray(content) 字符串转化为jeson集合实体类对象
                //结合
                template.process(map,out);
                //3.把html文件上传到minio中
                InputStream is = new ByteArrayInputStream(out.toString().getBytes());
                //三个参数(文件后缀,文件名,文件输入流)
                String filePath = fileStorageService
                        .uploadHtmlFile("", apArticle.getId() + ".html", is);
                //4.修改ap_article表，保存static_url字段
                apArticle.setStaticUrl(filePath);
                apArticle.setId(apArticle.getId());
                articleHomeService.updateById(apArticle);

                //发送消息，创建索引
                createArticleESIndex(apArticle,content,filePath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 送消息，创建索引
     * @param apArticle
     * @param content
     * @param path
     */
    private void createArticleESIndex(ApArticle apArticle, String content, String path) {
        SearchArticleVo vo = new SearchArticleVo();
        BeanUtils.copyProperties(apArticle,vo);
        vo.setContent(content);
        vo.setStaticUrl(path);

        kafkaTemplate.send(ArticleConstants.ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(vo));
}
}
